#include "Graph.h"
#include "iostream"

Graph::Graph()
{
	iVertex = 0;
	iComponent = 0;
}

void Graph::Resize(int n)
{
	Matrix.resize(iVertex);
	for(int i =0; i< Matrix.size(); i++)
	{
		Matrix[i].resize(iVertex);
	}
	cLabel.resize(iVertex);
}

/*void Graph::matrixInit()
{
	this->Matrix.resize(this->iVertex);
	for(int i=0; i<this->Matrix.size(); i++)
		this->Matrix[i].resize(this->iVertex);
}*/
/*
istream& operator>> (istream& inDevice, charArray2D& a)
{
	int temp;
	inDevice>>;
	matrixInit(a,n);
	for(int i=0; i<a.size();i++)
	{
		for(int j=0; j<a[i].size(); j++)
		{
			a[i][j] = 0;
			if(inDevice)
			{
				inDevice>> temp;
				a[i][j] = temp;
			}
		}
	}
	return inDevice;

}

ostream& operator<< (ostream& outDevice, charArray2D &a)
{
	for(int i=0; i<a.size(); i++)
	{
		for(int j=0; j<a[i].size(); j++)
		{
			outDevice<< (int)a[i][j]<< " ";
		}
		outDevice<< endl;
	}
	return outDevice;
}
*/
void Graph::Input(char *cFileName)
{
	ifstream fFile(cFileName);
	//fFile.open(cFileName);
	if(!fFile)
	{
		cout<<"Tap tin "<<cFileName<<" khong ton tai";
	}
	fFile>>iVertex;
	Resize(iVertex);
	//fFile.ignore();
	for(int i=0; i<Matrix.size();i++)
	{
		for(int j=0; j<Matrix.size();j++)
		{
			fFile>>Matrix[i][j];
			//fFile.ignore();
		}
	}
	fFile.close();
}

bool Graph::validGraph()
{
	for(int i=0; i<Matrix.size(); i++)
	{
		if(Matrix[i][i] != 0)
		{
			return false;
		}
		for(int j=0; j<Matrix.size(); j++)
		{
			if(Matrix[i][j] != 1)
				return false;
		}
	}
	return true;
}

int Graph::FindComponents()
{
	
	int Label =0;
	for(int i = 0; i <Matrix.size(); i++)
	{
		this->cLabel[i] = 0;
	}
	for(int j = 0; j <Matrix.size(); j++)
	{
		if(cLabel[j]==0)
		{
			Label++;
			Visit(j, Label);		
		}
		iComponent = Label;
	}
		return iComponent;
}

void Graph::Output(char *cFileName)
{
	ofstream fFile;
	fFile.open(cFileName);
	fFile << iComponent;
	for(int i=1; i<=iComponent; i++)
	{
		int s=0;
		for(int j=0; j< Matrix.size(); j++)
		{
			if(cLabel[j] == i )
				s++;
		}
		for(int j=0; j< Matrix.size(); j++)
		{
			if(cLabel[j] == i)
			{
				fFile << j+1 <<" ";
			}
		}
		fFile << endl;
	}
	fFile.close();
}

Graph::~Graph()
{
	
}